﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace dataDict
{
    /// <summary>
    ///  主调程序
    /// </summary>
    static class MainProgram
    {
        #region 声明与窗体显示相关的api
        /// <summary>
        /// 找到某个窗口与给出的类别名和窗口名相同窗口
        /// 非托管定义为：http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx
        /// </summary>
        /// <param name="lpClassName">类别名</param>
        /// <param name="lpWindowName">窗口名</param>
        /// <returns>成功找到返回窗口句柄,否则返回null</returns>
        [DllImport ( "user32.dll" )]
        public static extern IntPtr FindWindow ( string lpClassName , string lpWindowName );

        /// <summary>
        /// 切换到窗口并把窗口设入前台,类似 SetForegroundWindow方法的功能
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="fAltTab">True代表窗口正在通过Alt/Ctrl +Tab被切换</param>
        [DllImport ( "user32.dll " , SetLastError = true )]
        static extern void SwitchToThisWindow ( IntPtr hWnd , bool fAltTab );

        ///// <summary>
        /////  设置窗口的显示状态
        /////  Win32 函数定义为：http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
        ///// </summary>
        ///// <param name="hWnd">窗口句柄</param>
        ///// <param name="cmdShow">指示窗口如何被显示</param>
        ///// <returns>如果窗体之前是可见，返回值为非零；如果窗体之前被隐藏，返回值为零</returns>
        [DllImport ( "user32.dll" , EntryPoint = "ShowWindow" , CharSet = CharSet.Auto )]
        public static extern int ShowWindow ( IntPtr hwnd , int nCmdShow );
        public const int SW_RESTORE = 1;
        public static IntPtr formhwnd;
        #endregion
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main ( )
        {
            #region 检查程序是否已运行，如已运行则显示出主窗体
            Process currentproc = Process.GetCurrentProcess ( ); //取当时进程
            string WintTitle = Properties.Settings.Default.AppName;  //利用 Properties 设定主窗体的标题
            string proc = currentproc.ProcessName.Replace ( ".vshost" , string.Empty ); //取当前进程名名称，在开发状态进程名是显示为 xxxx.vshost.exe 形式的，因此要去除 .vshost
            Process[] processcollection = Process.GetProcessesByName ( proc );  //按当前进程名称查找已运行的进程
            if (processcollection.Length > 1) // 程序已运行
            {
                foreach (Process process in processcollection)
                {
                    if (process.Id != currentproc.Id) // 如果当前的进程ＩＤ与查找的进程ID不同，显示窗口
                    {
                        if (process.MainWindowHandle.ToInt32 ( ) == 0)  //针对程序缩小在托盘架上的处理
                        {
                            formhwnd = FindWindow ( null , WintTitle ); //是查找主窗口的标题，不是主窗口的内部名称
                            ShowWindow ( formhwnd , SW_RESTORE );
                            SwitchToThisWindow ( formhwnd , true );
                        }
                        else
                        {
                            SwitchToThisWindow ( process.MainWindowHandle , true );
                        }
                    }
                }
            }
            else
            {
                Application.EnableVisualStyles ( );
                Application.SetCompatibleTextRenderingDefault ( false );
                Application.Run ( new main ( ) );
            }
            #endregion
            
        }
    }
}